# backend/core/image_generator.py
from PIL import Image, ImageDraw
import random
import os
from pathlib import Path


def generate_basic_sliders(output_folder="static/captcha"):
    """生成基础滑块图片"""
    sliders_dir = Path(output_folder) / "sliders"
    sliders_dir.mkdir(parents=True, exist_ok=True)

    print(f"生成滑块图到目录: {sliders_dir.absolute()}")

    count = 0
    shapes = ['circle', 'square', 'star', 'triangle', 'diamond', 'heart']
    colors = [(255, 100, 100), (100, 255, 100), (100, 100, 255),
              (255, 255, 100), (255, 100, 255), (100, 255, 255)]

    for i, (shape, color) in enumerate(zip(shapes * 2, colors * 2)):
        if count >= 10:  # 最多生成10张
            break

        try:
            slider = Image.new('RGBA', (50, 30), (0, 0, 0, 0))
            draw = ImageDraw.Draw(slider)

            if shape == 'circle':
                draw.ellipse([5, 5, 45, 25], fill=color + (255,))
            elif shape == 'square':
                draw.rectangle([10, 5, 40, 25], fill=color + (255,))
            elif shape == 'star':
                points = [(25, 2), (30, 12), (40, 12), (32, 18),
                          (35, 28), (25, 22), (15, 28), (18, 18), (10, 12), (20, 12)]
                draw.polygon(points, fill=color + (255,))
            elif shape == 'triangle':
                draw.polygon([(25, 5), (45, 25), (5, 25)], fill=color + (255,))
            elif shape == 'diamond':
                draw.polygon([(25, 5), (40, 15), (25, 25), (10, 15)], fill=color + (255,))
            elif shape == 'heart':
                draw.ellipse([10, 5, 30, 15], fill=color + (255,))
                draw.ellipse([20, 5, 40, 15], fill=color + (255,))
                draw.polygon([(10, 10), (25, 25), (40, 10)], fill=color + (255,))

            # 添加边框
            draw.rectangle([0, 0, 49, 29], outline=(255, 255, 255, 200), width=2)

            output_path = sliders_dir / f"basic_slider_{count + 1:02d}.png"
            slider.save(output_path, "PNG")

            count += 1
            print(f"生成基础滑块图: {output_path.name}")

        except Exception as e:
            print(f"生成基础滑块图失败: {e}")

    return count


def generate_basic_backgrounds(output_folder="static/captcha"):
    """生成基础背景图片"""
    backgrounds_dir = Path(output_folder) / "backgrounds"
    backgrounds_dir.mkdir(parents=True, exist_ok=True)

    print(f"生成背景图到目录: {backgrounds_dir.absolute()}")

    count = 0
    for i in range(10):
        try:
            # 随机颜色背景
            bg_color = (
                random.randint(50, 200),
                random.randint(50, 200),
                random.randint(50, 200)
            )

            bg = Image.new('RGB', (300, 150), bg_color)
            draw = ImageDraw.Draw(bg)

            # 添加纹理
            for _ in range(100):
                x = random.randint(0, 300)
                y = random.randint(0, 150)
                size = random.randint(2, 10)
                color = (
                    random.randint(100, 255),
                    random.randint(100, 255),
                    random.randint(100, 255)
                )
                draw.ellipse([x, y, x + size, y + size], fill=color)

            output_path = backgrounds_dir / f"basic_bg_{i + 1:02d}.jpg"
            bg.save(output_path, "JPEG", quality=85)

            count += 1
            print(f"生成基础背景图: {output_path.name}")

        except Exception as e:
            print(f"生成基础背景图失败: {e}")

    return count


if __name__ == "__main__":
    generate_basic_sliders()
    generate_basic_backgrounds()